home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS15.ADF / PopCLI2 / popcli.c < prev    next >
C/C++ Source or Header  |  1988-04-20  |  9KB  |  293 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2. /* |_o_o|\\ Copyright (c) 1986 The Software Distillery.  All Rights Reserved */
  3. /* |. o.| || This program may not be distributed without the permission of   */
  4. /* | .  | || the authors.                                                    */
  5. /* | o  | ||    Dave Baker     Ed Burnette  Stan Chow    Jay Denebeim        */
  6. /* |  . |//     Gordon Keener  Jack Rouse   John Toebes  Doug Walker         */
  7. /* ======          BBS:(919)-471-6436      VOICE:(919)-469-4210              */ 
  8. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  9. /*
  10.  * VERY loosely based on the input.device example by Rob Peck, 12/1/85
  11.  */
  12.  
  13. /* * * * * * * * * INCLUDE FILES * * * * * * * * * * * */
  14. #include <exec/types.h>
  15. #include <exec/nodes.h>
  16. #include <exec/lists.h>
  17. #include <exec/memory.h>
  18. #include <exec/interrupts.h>
  19. #include <exec/ports.h>
  20. #include <exec/libraries.h>
  21. #include <exec/io.h>
  22. #include <exec/tasks.h>
  23. #include <exec/execbase.h>
  24. #include <exec/devices.h>
  25. #include <devices/timer.h>
  26. #include <devices/input.h>
  27. #include <devices/inputevent.h>
  28. #include <intuition/intuition.h>
  29. #include <libraries/dos.h>
  30. #include <graphics/gfxmacros.h>
  31. #include <hardware/custom.h>
  32. #include <hardware/dmabits.h>
  33.  
  34. /* * * * * * * * * * * CONSTANTS * * * * * * * * * * * * */
  35. #define TIMEINTERVAL 1L      /* in seconds */
  36. #define DEFLIMIT     300     /* two minute timeout */
  37. #define ESCAPEKEY    0x45
  38. #define DEFCMD    "NEWCLI >NIL: <NIL:"
  39. #define BANNER "¢0;33mPOPCLI II¢0m by John Toebes - Copyright ⌐ 1986 The Software Distillery\n 235 Trillingham Ln, Cary NC 27511   BBS:(919)-471-6436\n"
  40. #define BANNER1 "Usage: ¢1mRUN POPCLI¢0m [secs [command]]\nsecs is number of seconds before blanking screen\ncommand is to be executed when Left Amiga-Escape is pressed\n"
  41. /* * * * * * * * * * * GLOBAL VARIABLES * * * * * * * * * */
  42. typedef struct
  43.    {
  44.    struct task          *buddy;
  45.    ULONG                 createclisig;
  46.    ULONG                 unblanksig;
  47.    ULONG                 noevents;
  48.    struct Screen        *blankscreen;
  49.    } GLOBAL_DATA;
  50.  
  51. /************************************************************************/
  52. /* the handler subroutine - called through the handler stub             */
  53. /************************************************************************/
  54. struct InputEvent *myhandler(ev, gptr)
  55. struct InputEvent *ev;      /* and a pointer to a list of events */
  56. register GLOBAL_DATA *gptr;      /* Everything we need to know about */
  57.    {
  58.    register struct InputEvent *ep, *laste;
  59.  
  60.    /* run down the list of events to see if they pressed the magic button */
  61.    for (ep = ev, laste = NULL; ep != NULL; ep = ep->ie_NextEvent)
  62.       {
  63.       if ((ep->ie_Class == IECLASS_RAWKEY) &&
  64.           (ep->ie_Code  == ESCAPEKEY)         &&
  65.           (ep->ie_Qualifier & IEQUALIFIER_LCOMMAND))
  66.          {
  67.          /* we can handle this event so take it off the chain */
  68.          if (laste == NULL)
  69.             ev = ep->ie_NextEvent;
  70.          else
  71.             laste->ie_NextEvent = ep->ie_NextEvent;
  72.          /* now tell him to create the new cli */
  73.          Signal(gptr->buddy, gptr->createclisig);
  74.          }
  75.       else
  76.          laste = ep;
  77.  
  78.       if (ep->ie_Class != IECLASS_TIMER)
  79.          {
  80.          gptr->noevents = 0;
  81.          if (gptr->blankscreen != NULL)
  82.             Signal(gptr->buddy, gptr->unblanksig);
  83.          }
  84.       }
  85.  
  86.    /* pass on the pointer to the event */
  87.    return(ev);
  88.    }
  89.  
  90. /* * * * * * * * * * * EXTERNAL ROUTINES * * * * * * * * * */
  91. struct IntuitionBase *IntuitionBase = NULL;
  92. struct GfxBase       *GfxBase = NULL;
  93. long                  DOSBase = 0;
  94. extern struct Custom custom;
  95. struct NewScreen      NewScreen = 
  96.    { 0, 0, 320, 30, 1, 0, 1, NULL, CUSTOMSCREEN, NULL, NULL, NULL, NULL };
  97.  
  98. extern APTR             AllocMem();
  99. extern struct MsgPort  *CreatePort();
  100. extern struct IOStdReq *CreateStdIO();
  101. extern struct Screen   *OpenScreen();
  102. extern void             HandlerInterface();
  103. extern struct task     *FindTask();
  104.  
  105. /************************************************************************/
  106. /* Queue a timer to go off in a given number of seconds                 */
  107. /************************************************************************/
  108. void QueueTimer(tr,seconds)
  109. struct timerequest *tr;
  110. ULONG seconds;
  111.    {
  112.    tr->tr_node.io_Command = TR_ADDREQUEST;   /* add a new timer request */
  113.    tr->tr_time.tv_secs =  seconds;            /* seconds */
  114.    tr->tr_time.tv_micro = 0;
  115.    SendIO( tr );
  116.    }
  117.  
  118. /************************************************************************/
  119. /* the main program to do the popcli stuff                              */
  120. /************************************************************************/
  121. void _main(cmd)
  122. char *cmd;
  123.    {
  124.    long  intlimit = 0, nullfh;
  125.    ULONG sig, timersig;
  126.    struct timerequest *timerreq;
  127.    struct MsgPort     *timerport;
  128.    struct MsgPort     *inputDevPort;
  129.    struct IOStdReq    *inputRequestBlock;
  130.    struct Interrupt      handlerStuff;
  131.    GLOBAL_DATA global;
  132.  
  133.    global.blankscreen = NULL;
  134.  
  135.    global.buddy = FindTask(0);
  136.    global.noevents = 0;
  137.  
  138.    if (cmd)
  139.       {
  140.       /* ugh we are reusing a variable here - so what - it saves bytes */
  141.       sig = Output();
  142.       Write(sig, BANNER, sizeof(BANNER));
  143.  
  144.       /* skip over any leading spaces in the command line */
  145.       while(*cmd == ' ')
  146.          cmd++;
  147.  
  148.       /* see if they specified a limit */
  149.       if (*cmd)
  150.          {
  151.          while ((*cmd >= '0') && (*cmd <= '9'))
  152.             intlimit = ((short)intlimit*(short)10) + *cmd++ - '0';
  153.          }
  154.  
  155.       if (intlimit <= 0)
  156.          {
  157.          Write(sig, BANNER1, sizeof(BANNER1));
  158.          intlimit = DEFLIMIT;
  159.          }
  160.  
  161.       /* now skip over any more leading spaces */
  162.       while(*cmd == ' ')
  163.          cmd++;
  164.  
  165.       if (*cmd <= ' ')
  166.          cmd = DEFCMD;
  167.  
  168.       if (sig != Input())
  169.          Close(Input());
  170.       Close(sig);
  171.       }
  172.    else
  173.       {
  174.       intlimit = DEFLIMIT;
  175.       cmd = DEFCMD;
  176.       }
  177.  
  178.    /* set the input and output streams to 0 so execute doen't complain */
  179.    nullfh = Open("NIL:", MODE_NEWFILE);
  180.    SetTaskPri( global.buddy, 20);
  181.  
  182.    if ((inputDevPort = CreatePort(0,0)) == NULL) /* for input device */
  183.       goto abort0;
  184.  
  185.    if ((inputRequestBlock = CreateStdIO(inputDevPort)) == 0)
  186.       goto abort1;
  187.  
  188.    if ((timerport = CreatePort(0,0)) == NULL)
  189.       goto abort1;
  190.  
  191.    if ((timerreq = (struct timerequest *)
  192.                     AllocMem(sizeof(struct timerequest),
  193.                              MEMF_CLEAR | MEMF_PUBLIC)) == NULL)
  194.       goto abort2;
  195.  
  196.    timerreq->tr_node.io_Message.mn_Node.ln_Type = NT_MESSAGE;
  197.    timerreq->tr_node.io_Message.mn_Node.ln_Pri = 0;
  198.    timerreq->tr_node.io_Message.mn_ReplyPort = timerport;
  199.  
  200.    if (OpenDevice(TIMERNAME, UNIT_VBLANK, timerreq, 0))
  201.       goto abort;
  202.  
  203.    timersig = (1 << timerport->mp_SigBit);
  204.  
  205.    if ((sig = AllocSignal(-1)) == -1)
  206.       goto abort;
  207.  
  208.    global.createclisig = 1 << sig;
  209.  
  210.    if ((sig = AllocSignal(-1)) == -1)
  211.       goto abort;
  212.  
  213.    global.unblanksig = 1 << sig;
  214.  
  215.    if ((GfxBase = (struct GfxBase *)
  216.                   OpenLibrary("graphics.library", 0)) == NULL)
  217.       goto abort;
  218.  
  219.    if ((IntuitionBase = (struct IntuitionBase *)
  220.                         OpenLibrary("intuition.library", 0)) == NULL)
  221.       goto abort;
  222.  
  223.    handlerStuff.is_Data = (APTR)&global;
  224.    handlerStuff.is_Code = HandlerInterface;
  225.    handlerStuff.is_Node.ln_Pri = 51;
  226.  
  227.    if (OpenDevice("input.device",0,inputRequestBlock,0))
  228.       goto abort;
  229.  
  230.    inputRequestBlock->io_Command = IND_ADDHANDLER;
  231.    inputRequestBlock->io_Data    = (APTR)&handlerStuff;
  232.  
  233.    DoIO(inputRequestBlock);
  234.  
  235.    QueueTimer(timerreq, TIMEINTERVAL);
  236.  
  237.    for(;;)         /* FOREVER */
  238.       {
  239.       sig = Wait( global.createclisig | global.unblanksig | timersig );
  240.  
  241.       if ((sig & global.unblanksig) && (global.blankscreen != NULL))
  242.          {
  243.          (void) CloseScreen(global.blankscreen);
  244.      ON_DISPLAY
  245.          global.blankscreen = NULL;
  246.          }
  247.  
  248.       if (sig & global.createclisig)
  249.          {
  250.          WBenchToFront();
  251.          (void)Execute(cmd,nullfh,nullfh);
  252.          }
  253.  
  254.       if (sig & timersig)
  255.          {
  256.          /* get rid of the message */
  257.          (void)GetMsg(timerport);
  258.          QueueTimer(timerreq, TIMEINTERVAL);
  259.  
  260.          if ((global.noevents++ >= intlimit) && (global.blankscreen == NULL))
  261.             {
  262.             if ( (global.blankscreen = OpenScreen(&NewScreen)) != NULL)
  263.                {
  264.                SetRGB4(&(global.blankscreen->ViewPort), 0, 0, 0, 0);
  265.                OFF_DISPLAY
  266.                }
  267.             }
  268.          }
  269.       }
  270.  
  271. abort:
  272.    CloseDevice(timerreq);
  273. abort3:
  274. /* timerreq->tr_node.io_Message.mn_Node.ln_Type = 0xff;
  275.  * timerreq->tr_node.io_Device = (struct Device *) -1;
  276.  * timerreq->tr_node.io_Unit = (struct Unit *) -1;
  277.  */
  278.  
  279.    FreeMem (timerreq, sizeof(struct timerequest));
  280. abort2:
  281.    DeletePort(timerport);
  282. abort1:
  283.    if (IntuitionBase != NULL)
  284.       CloseLibrary(IntuitionBase);
  285.  
  286.    if (GfxBase != NULL)
  287.       CloseLibrary(GfxBase);
  288.  
  289.    DeletePort(inputDevPort);
  290. abort0:
  291.    XCEXIT(-1);
  292.    }
  293.